

import 'package:elevator/pages/home/all/detail/detail_view.dart';
import 'package:elevator/pages/home/all/detail_new/detail_new_view.dart';
import 'package:elevator/utils/utils_logger.dart';
import 'package:elevator/widgets/globel_widget.dart';
import 'package:elevator/widgets/spirit/spirit_view.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../get/intl_keys.dart';
import '../../../widgets/hor_single_choice_list/hor_single_choice_list_view.dart';
import 'home_logic.dart';

class HomePage extends StatefulWidget {
  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin, WidgetsBindingObserver {

  final logic = Get.put(HomeLogic());
  final state = Get.find<HomeLogic>().state;

  @override
  Widget build(BuildContext context) {

    return WillPopScope(
        onWillPop: logic.willPop,
        child:Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title:
              const Image(image: AssetImage("images/logo.png"), width: 100.0),
          actions: [
            IconButton(
              ///icon: const Icon(Icons.person,size: 36,),
              icon:Image.asset("images/person.png"),
              onPressed: () {logic.goToSettingPage();},
            ),
          ],
        ),
        body: Container(
            width: double.infinity,
            height: double.infinity,
            decoration: const BoxDecoration(
              image: DecorationImage(
                image: AssetImage('images/app_bg.png'),
                fit: BoxFit.cover,
              ),
            ),
            child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[
                  Container(
                    ///楼盘 工作区 横向list
                    margin: const EdgeInsets.fromLTRB(0, 10, 0, 0),
                    height: 56,
                    color: Colors.white,
                    child: buildAreaListView(),
                  ),
                  Container(
                    ///电梯组list
                    margin: const EdgeInsets.fromLTRB(0, 10, 0, 10),
                    height: 54,
                    color: Colors.white,
                    child: buildGroupListView()
                  ),
                  Flexible(
                      /// 电梯状态列表布局
                      child: Container(
                        decoration: BoxDecoration(
                          // 背景色
                          border: Border.all(color: Colors.white, width: 0.5),
                          // border
                          borderRadius: BorderRadius.circular((6)), // 圆角
                        ),
                        margin: const EdgeInsets.fromLTRB(4, 0, 4, 0),
                        child: createLiftListView(),
                  )),
                  createBottomButton(context)
                ]))));
  }

  ///中间电梯信息列表
  createLiftListView(){
    return GetBuilder<HomeLogic>(
      id: HomeLogic.ID_LIFT_LISTVIEW,
      builder: (HomeLogic controller) {
        return ListView.builder(
          itemCount: state.allLiftList.isEmpty? state.selectGroupInfo.value.liftList.length:state.allLiftList.length,
          itemBuilder: (context, index) {
            var data = state.allLiftList.isEmpty? state.selectGroupInfo.value.liftList[index]:state.allLiftList[index];
            var alarmList = data.alermList;
            return Column(
              children: [
                spaceHeight(height: 10),
                SpiritComponent(
                  onTap: () {
                    logd('onTap home view');
                    // Get.to(DetailPage());
                    Get.toNamed("/lift_detail",arguments: {"buildInfo":state.selectBuildInfo.value, "liftInfo":data,"psAuth":state.user.psAuth!,"user":state.user});
                  },
                  onLongPress: () {
                    logd('onLongPress');
                  },
                  liftType: data.liftType,
                  liftOpenType:data.liftOpenType,
                  curStates: data.currentState,
                  lLeftText: data.liftName,
                  lRightText: data.currentAddr,
                  widgets: getAlermInfoWidgets(alarmList),
                ),
                spaceHeight(height: 10),
              ],
            );
          },
        );
      }

    );

  }

  ///底部按钮布局
  createBottomButton(BuildContext context){
    return Padding(
        padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
        child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              ElevatedButton(
                style: ButtonStyle(
                    shape: MaterialStateProperty.all<
                        RoundedRectangleBorder>(
                      RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(8.0),
                      ),
                    ),
                    padding: MaterialStateProperty.all(
                        const EdgeInsets.all(5)),
                    foregroundColor:
                    MaterialStateProperty.all(Colors.black),
                    backgroundColor:
                    MaterialStateProperty.all(Colors.white),
                    textStyle: MaterialStateProperty.all(
                        const TextStyle(fontSize: 20)),
                    overlayColor: MaterialStateProperty.all(
                        const Color(0xFFBDBDBD))),

                onPressed: () => logic.clickAll(context),
                child: Text(IntlKeys.all.tr), //选中的颜色字体颜色
              ),
              ElevatedButton(
                style: ButtonStyle(
                    shape: MaterialStateProperty.all<
                        RoundedRectangleBorder>(
                      RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(8.0),
                      ),
                    ),
                    padding: MaterialStateProperty.all(
                        const EdgeInsets.all(5)),
                    foregroundColor:
                    MaterialStateProperty.all(Colors.black),
                    backgroundColor:
                    MaterialStateProperty.all(Colors.white),
                    textStyle: MaterialStateProperty.all(
                        const TextStyle(fontSize: 20)),
                    overlayColor: MaterialStateProperty.all(
                        const Color(0xFFBDBDBD))),

                onPressed: () => logic.clickSearch(context),
                child: const Icon(Icons.search), //选中的颜色字体颜色
              ),
            ]));
  }

  ///构建区域/楼盘横向列表 ListView
  buildAreaListView() {
    debugPrint("------------buildAreaListView:${state.buildList.length}");
    return Obx(() =>HorSingleChoiceList(
        datas: state.buildList.value,
        selectedPositionId: state.selectBuildPositionId.value,
        onSelectChanged: (int index){
          logic.selectIdBuildChanged(index);
        }));
  }

  ///构建区域/楼盘横向列表 ListView
  buildGroupListView() {
    debugPrint("------------buildGroupListView:${state.buildList.length}");
    return Obx(() =>HorSingleChoiceList(
        datas: state.selectBuildInfo.value.groupList,
        selectedPositionId: state.selectGroupPositionId.value,
        selelctColor: Colors.red,
        onSelectChanged: (int index){
          logic.selectIdGroupChanged(index);
        }));
    ///懒加载模式构建
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    logd('lifeCycle----->$state');
    switch(state) {
      case AppLifecycleState.paused:
      // BackgroundFetch.scheduleTask(TaskConfig(
      //     taskId: 'com.lee.elevator',
      //     delay: 60 * 60 * 1000  //  毫秒
      // ));

        break;

      case AppLifecycleState.detached:
      // TODO: Handle this case.
        break;
      case AppLifecycleState.resumed:
      // TODO: Handle this case.

        break;
      case AppLifecycleState.inactive:
      // TODO: Handle this case.
        break;
      case AppLifecycleState.hidden:
      // TODO: Handle this case.
        break;
    }
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

}
